import { Component, OnInit } from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder } from '@angular/forms';
import { _HttpClient } from '@core/services/http.client';

@Component({
  selector: 'app-teacher',
  templateUrl: './teacher.component.html'
})
export class TeacherComponent implements OnInit {
    selectValue;
    options = [
        { value: '', label: '请选择' },
        { value: '0', label: '在职' },
        { value: '1', label: '离职' }
    ];
    saveState = '';
    searchValue = '';
    serchName = {};
    popTitle = '';
    ids = [];
    loading = false;
    modalVisible1 = false;
    Tname = '';
    Tschool = '';
    Tphone = '';
    Temail = '';
    _allChecked = false;
    _indeterminate = false;
    _checkTable = [];
    _displayData = [];
    _checkLength = 0;
    _loading = false;
    formNews = {};
    data = [];
    Status = [];
    sta = '';
    ngOnInit() {
        this.getData();
    }

    modal (state, data) {
        if (state === 'add') {
            this.Tname = '';
            this.Tschool = '';
            this.Tphone = '';
            this.Temail = '';
            this.selectValue = '';
            this.popTitle = '新建教师';
            this.modalVisible1 = true;
            this.saveState = 'add';
        }else if (state === 'update') {
            this.modalVisible1 = true;
            this.popTitle = '修改教师';
            this.Tname = data.T_name;
            this.Tschool = data.T_college;
            this.Tphone = data.T_phone;
            this.Temail = data.T_email;
            this.selectValue = data.T_status;
            this.saveState = 'update';
        }

    }

    getData() {
        // this._loading = true;
        const type = {pi: 1, ps: 10};
        this.http.post('EduManagement/teacher/list', type).subscribe(res => {
            console.log(res);
            // this._loading = false;
            // this.data = res;
        });
        this.data = [
            {
                T_id    : '1',
                T_name   : '张硕1',
                T_college : 'aaa',
                T_phone  : '13633221000',
                T_email  : '123@qq.com',
                T_status  : '1'
            },
            {
                T_id    : '2',
                T_name   : '张硕2',
                T_college : 'aaa',
                T_phone  : '13633221000',
                T_email  : '123@qq.com',
                T_status  : '0'
            },
            {
                T_id    : '3',
                T_name   : '张硕3',
                T_college : 'aaa',
                T_phone  : '13633221000',
                T_email  : '123@qq.com',
                T_status  : '0'
            },
            {
                T_id    : '4',
                T_name   : '张硕3',
                T_college : 'aaa',
                T_phone  : '13633221000',
                T_email  : '123@qq.com',
                T_status  : '1'
            },
            {
                T_id    : '5',
                T_name   : '张硕3',
                T_college : 'aaa',
                T_phone  : '13633221000',
                T_email  : '123@qq.com',
                T_status  : '1'
            }
        ];
        this.data.map(d => {
            if (d.T_status == '0') {
                this.sta = '在职';
            } else {
                this.sta = '离职';
            }
            this.Status.push(this.sta);
        });
    }

    save() {
        this.loading = true;
        setTimeout(() => {
            this.modalVisible1 = false;
            this.loading = false;
            this.createMessage('success', '操作成功！');
        }, 500);
        if (this.saveState === 'add') {
            this.formNews = {T_name: this.Tname, T_college: this.Tschool, T_phone: this.Tphone, T_email: this.Temail, T_status: this.selectValue }
        }else {
            this.formNews = {T_id: '', T_name: this.Tname, T_college: this.Tschool, T_phone: this.Tphone, T_email: this.Temail, T_status: this.selectValue }
        }
        this.http.post('EduManagement/teacher/' + this.saveState, this.formNews).subscribe(res => {
            this.loading = false;
            this.getData();
            console.log(res);
            // this.createMessage('success', '操作成功！');
        });

    }

    search () {
        this.serchName = {T_name: this.searchValue}
        this.http.post('', this.serchName).subscribe(res => {
            console.log(res);
        });
    }

    showConfirm = (data) => {
        this.confirmServ.confirm({
            title  : '您是否确认要删除这项内容',
            onOk() {
                const T_id = {T_id: data.T_id};
                // this.http.post('EduManagement/teacher/del', T_id).subscribe(res => {
                //     console.log(res);
                // this.createMessage('success', '删除成功！');
                // });
            },
            onCancel() {
            }
        });
    }

    _displayDataChange($event) {
        this._displayData = $event;
        this._refreshStatus();
    }

    _refreshStatus() {
        const allChecked = this._displayData.every(value => value.checked === true);
        const allUnChecked = this._displayData.every(value => !value.checked);
        const checkTable = this._displayData.filter(value => value.checked);
        this._allChecked = allChecked;
        this._indeterminate = (!allChecked) && (!allUnChecked);
        this._checkTable = checkTable;
        this._checkLength = checkTable.length;

    }

    _checkAll(value) {
        if (value) {
            this._displayData.forEach(data => {
                data.checked = true;
            });
        } else {
            this._displayData.forEach(data => {
                data.checked = false;
            });
        }
        this._refreshStatus();
    }

    allDelete () {
        this._checkTable.map(m => {
            this.ids.push(m.T_id);
        });
        this.confirmServ.confirm({
            title  : '您是否确认要批量删除选中的' + this._checkLength + '项内容？',
            onOk: () => {
                console.log(this.ids);
                // this.http.post('EduManagement/teacher/del', {T_id: this.ids}).subscribe(res => {
                //     console.log(res);
                // this.createMessage('success', '删除成功！');
                // });
            },
            onCancel() {
            }
        });
    }

    info(contentTpl, data, status) {
        this.Tname = data.T_name;
        this.Tschool = data.T_college;
        this.Tphone = data.T_phone;
        this.Temail = data.T_email;
        this.selectValue = status;
        this.confirmServ.info({
            title: '查看详情',
            content: contentTpl
        });
    }

    createMessage = (type, text) => {
        this._message.create(type, `${text}`);
    }

    constructor(
        private fb: FormBuilder,
        private confirmServ: NzModalService,
        private http: _HttpClient,
        private _message: NzMessageService
    ) {}
}
